package com.czxy.servlet;

import cn.itcast.servlet.BaseServlet;
import cn.itcast.vcode.VerifyCode;
import com.czxy.domain.User;
import com.czxy.service.UserService;

import javax.servlet.annotation.WebServlet;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {

    public String logOut(){
        //1.清理购物车
        //2.清除session
        getSession().invalidate();
        //3.页面跳转
        return "redirect:/jsp/login.jsp";
    }


    /**
     * 登录逻辑
     * @return
     */
    public String login(){
        //1.接受参数
        User user = toBean(User.class);
        //2.调用Service获取结果
        UserService service = new UserService();
        User loginU = service.login(user);
        //3.根据结果做出响应
        if(loginU == null){
            //登录失败
            //存失败信息
            getRequest().setAttribute("loginInfo", "登录失败用户名或密码错误");
            getRequest().setAttribute("user", user);
            //请求转发
            return "/jsp/login.jsp";

        }else{
            //登录成功
            //存session
            getSession().setAttribute("loginU", loginU);
            //重定向
            return "redirect:/jsp/index.jsp";
        }
    }

    /**
     * 验证码校验逻辑
     * @throws IOException
     */
    public void checkVcCode() throws IOException {
        //1.获取前端传递的vcText
        String vcText = getRequest().getParameter("vcText");
        //2.获取session中的vcCode
        String vcCode = (String) getSession().getAttribute("vcCode");
        //3.移除session中的vcCode
        getSession().removeAttribute("vcCode");

        //4.比较是否相同给出响应
        if(vcText==null || vcCode==null ||"".equals(vcText) || "".equals(vcCode)){

            //校验失败
            //{"message":"验证码校验失败","flag":false}
            getResponse().getWriter().write("{\"message\":\"验证码校验失败\",\"flag\":false}");
            return;
        }

        if(vcText.equalsIgnoreCase(vcCode)){
            //{"message":"验证码校验成功","flag":true}
            getResponse().getWriter().write("{\"message\":\"验证码校验成功\",\"flag\":true}");
        }else{
            //{"message":"验证码校验失败","flag":false}
            getResponse().getWriter().write("{\"message\":\"验证码校验失败\",\"flag\":false}");
        }


    }



    /**
     * 生成验证码图片
     * @throws IOException
     */
    public void getVcImg() throws IOException {
        //1.生成验证码图片
        VerifyCode vc = new VerifyCode();
        BufferedImage image = vc.getImage();
        //2.获取对应的验证码码值 存储到session
        String vcCode = vc.getText();
        getSession().setAttribute("vcCode", vcCode);
        System.out.println(vcCode);
        //3.把图片响应给前端
        VerifyCode.output(image, getResponse().getOutputStream());
    }



    public String register(){
        //1.接受参数
        User user = toBean(User.class);
        //2.完成注册
        UserService userService = new UserService();
        userService.register(user);
        //3.响应
        return "redirect:/jsp/login.jsp";
    }

    /**
     * 注册的时候  校验用户名
     * @throws IOException
     */
    public void checkUsername() throws IOException {
        //1.接收 username
        String username = getRequest().getParameter("username");
        //2.调用Service获取结果
        UserService service = new UserService();
        List<User> list= service.findUserByUsername(username);
        //3.根据结果发送json数据
        if(list.size() == 0){
            //用户名可用
            //{"message":"用户名可用","flag":true}


            getResponse().getWriter().write("{\"message\":\"用户名可用\",\"flag\":true}");
        }else{
            //用户名 不可用
            //{"message":"用户名不可用","flag":false}
            getResponse().getWriter().write("{\"message\":\"用户名不可用\",\"flag\":false}");
        }
    }
}
